package com.repair.controller;

import com.repair.entity.Announcement;
import com.repair.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/announcements")
public class AnnouncementController {
    
    @Autowired
    private AnnouncementService announcementService;
    
    @PostMapping
    public ResponseEntity<Announcement> create(@RequestBody Announcement announcement,
                                            @AuthenticationPrincipal UserDetails userDetails) {
        // 设置创建人ID
        announcement.setCreatedBy(Long.parseLong(userDetails.getUsername()));
        return ResponseEntity.ok(announcementService.create(announcement));
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<Void> update(@PathVariable Long id,
                                     @RequestBody Announcement announcement) {
        announcement.setId(id);
        announcementService.update(announcement);
        return ResponseEntity.ok().build();
    }
    
    @PutMapping("/{id}/publish")
    public ResponseEntity<Void> publish(@PathVariable Long id) {
        announcementService.publish(id);
        return ResponseEntity.ok().build();
    }
    
    @PutMapping("/{id}/offline")
    public ResponseEntity<Void> offline(@PathVariable Long id) {
        announcementService.offline(id);
        return ResponseEntity.ok().build();
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<Announcement> getById(@PathVariable Long id,
                                             @AuthenticationPrincipal UserDetails userDetails) {
        Announcement announcement = announcementService.getById(id);
        if (announcement != null) {
            // 记录阅读状态
            announcementService.read(id, Long.parseLong(userDetails.getUsername()));
        }
        return ResponseEntity.ok(announcement);
    }
    
    @GetMapping("/latest")
    public ResponseEntity<List<Announcement>> getLatest(@RequestParam(defaultValue = "5") int limit) {
        return ResponseEntity.ok(announcementService.getLatest(limit));
    }
    
    @GetMapping("/user")
    public ResponseEntity<List<Announcement>> getByUserType(@RequestParam Integer userType) {
        return ResponseEntity.ok(announcementService.getByUserType(userType));
    }
    
    @GetMapping
    public ResponseEntity<List<Announcement>> getAll() {
        return ResponseEntity.ok(announcementService.getAll());
    }
    
    @GetMapping("/{id}/read")
    public ResponseEntity<Boolean> isRead(@PathVariable Long id,
                                        @AuthenticationPrincipal UserDetails userDetails) {
        return ResponseEntity.ok(
            announcementService.isRead(id, Long.parseLong(userDetails.getUsername()))
        );
    }
} 